/**
 * Copyright (c) 2010 by Kevin Bierhoff.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *    1. Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *    3. Neither the names of the authors nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.googlecode.syper.javasyp.formulae;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.InfixExpression;

/**
 * @author kevin
 * @since Jan 30, 2010
 *
 */
public interface Formula {

	public boolean isBoolean();

	public <T> T dispatch(FormulaVisitor<T> visitor);

	/** Visit a formula with this visitor to figure out if it is of integer type */
	public static final FormulaVisitor<Boolean> IS_INTEGER = new FormulaVisitor<Boolean>() {

		private final Set<InfixExpression.Operator> numericalOps;
		{
			// there's only one instance of this class
			// so no biggie to create operator set in constructor
			numericalOps = new HashSet<InfixExpression.Operator>();
			numericalOps.add(InfixExpression.Operator.AND);
			numericalOps.add(InfixExpression.Operator.DIVIDE);
			numericalOps.add(InfixExpression.Operator.LEFT_SHIFT);
			numericalOps.add(InfixExpression.Operator.MINUS);
			numericalOps.add(InfixExpression.Operator.OR);
			numericalOps.add(InfixExpression.Operator.PLUS);
			numericalOps.add(InfixExpression.Operator.REMAINDER);
			numericalOps.add(InfixExpression.Operator.RIGHT_SHIFT_SIGNED);
			numericalOps.add(InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED);
			numericalOps.add(InfixExpression.Operator.TIMES);
			numericalOps.add(InfixExpression.Operator.XOR);
		}

		@Override
		protected Boolean defaultVisit(Formula f) {
			return false;
		}

		private boolean isIntegerType(ITypeBinding t) {
			return t != null && TypingUtil.isInteger(t);
		}

		@Override
		public Boolean visit(Field f) {
			return isIntegerType(f.getType());
		}

		@Override
		public Boolean visit(Fun f) {
			return isIntegerType(f.getType());
		}

		@Override
		public Boolean visit(IfThenElse f) {
			return f.getThenFormula().dispatch(this) && f.getElseFormula().dispatch(this);
		}

		@Override
		public Boolean visit(Infix f) {
			// test operators
			// some operators work for booleans and integers (&, |, ^)
			// others work for floats and integers (+-*/)
			// shifts and remainder are integers only but for simplicity always check operands
			return numericalOps.contains(f.getOp())
				&& f.getLhs().dispatch(this)
				&& f.getRhs().dispatch(this);
		}

		@Override
		public Boolean visit(Literal f) {
			Object v = f.getValue();
			return v instanceof Integer || v instanceof Long || v instanceof Byte || v instanceof Short;
		}

		@Override
		public Boolean visit(Local f) {
			return !f.isMeta() && isIntegerType(f.getType());
		}

		@Override
		public Boolean visit(Pre f) {
			return f.getAged().dispatch(this);
		}

		@Override
		public Boolean visit(Prefix f) {
			return f.getRhs().dispatch(this);
		}

		@Override
		public Boolean visit(Select f) {
			return isIntegerType(f.getType());
		}
	};
}
